/**
 * @program: malll
 * @description:
 * @author: Mr.Xu
 * @create: 2020-11-28 20:33
 **/

package com.bobo.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {
    /**
     * 初始化方法，可以获取注解上的详细信息
     * @param constraintAnnotation
     */
    Set<Integer> set = new HashSet<Integer>();
    public void initialize(ListValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        for (int val : vals) {
            set.add(val);
        }
    }

    /**
     * @param value 需要校验的值
     * @param context
     * @return
     */
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}